#include "esp8266_work.h"
          
/*************************************************************
    配网流程
    1、将无线设备初始化为AP模式，默认设置为
    2、AT+CWJAP="CMCC-LI","092413131li"
    // 配置 MQTT 用户属性
    // AT+MQTTUSERCFG=0,1,"8bf209cd00704760b7a60b2f71be9d8c","test","12345678",0,0,""
    // 配置 MQTT 连接属性
    // AT+MQTTCONNCFG=0,120,0,"","",0,0
    // 连接/查询 MQTT Broker
    // AT+MQTTCONN=0,"106.12.9.213",1883,0
    // 发布主题
    // {"deviceNum":"E8DB84933056","categoryId":2,"firmwareVersion":"1.0","ownerId":"1"}
    // AT+MQTTPUB=0,"device_info","{\"deviceNum\":\"E8DB8493312\"\\,\"categoryId\":1\\,\"firmwareVersion\":\"1.0\"\\,\"ownerId\":\"1\"}",0,0
    // AT+MQTTPUB=0,"device_info","{\"deviceNum\":\"E8DB8493312\"\\,\"categoryId\":1\\,\"firmwareVersion\":\"1.0\"\\,\"ownerId\":\"1\"}",0,0\r\n
    // 订阅/查询主题
    // AT+MQTTSUB=0,"status/set/E8DB84933056",0
    // 取消订阅
    // AT+MQTTUNSUB=0,"status/set/E8DB84933050"
    // 关闭连接, 释放资源
    // AT+MQTTCLEAN=0
************************************************************/

uint8_t  Wssid[20]     = "";
uint8_t  Wpassword[20] = "";

#ifdef MQTT_SCode
#define MQTT_OWNER_ID       "1"
#define MQTT_CATEGORY       6
#define MQTT_VERSION        "1.0"

char MQTT_ServerIP[20] = "106.12.9.213";
uint32_t MQTT_ServerPort = 1883;
static char MQTT_DeviceID[20]  = "E8DB84933299";
static char MQTT_ClientID[100] = "user";
static char MQTT_Username[20]  = "test";
static char MQTT_Password[20]  = "123456";
#endif

uint8_t PublishData[500] = {0};
int len = 0;

int ESP8266_NetStatus(int event) {
	switch (event)
	{
		case WIFI_DISCONNECT:
			
		break;
		case WIFI_GOT_IP:
				DevParam.NetWorkFlow = 5;
		break;
		case WIFI_SERVER_CONNECTED:
			
		break;
		default:
			
		break;
	}
	return 0;
}

void ESP8266_NetWorkFlow(void) {
    if(DevParam.NetWorkFlow == 0)    {
        if(DevParam.ShortRunPhase == RunPhase_Runing)
		{			
            DevParam.NetWorkFlow = 200;  
		}		
    }
    // 第一步，初始化为AP
    if(DevParam.NetWorkFlow == 200)    {
        ESP8266_APInit("XiaoYi_IOT_AirBox", "asdqwe9867");
        DevParam.NetWorkFlow = 2;
    }
    // 第一步，开启SmartConfig模式
    if(DevParam.NetWorkFlow == 1)    {
		ESP8266_STAInit();
        ESP8266_Start_SmartConfig();
        DevParam.NetWorkFlow = 212;
    }
    // 第二步，持续发送UDP广播
    else if(DevParam.NetWorkFlow == 2)    {
        if(DevParam.ESP8266SendTime >= 2000)
        {
            ESP8266_SendStr("{\"code\":0,\"mac\":\"%s\",\"msg\":\"start connect\"}", DevParam.macbuf);
            DevParam.ESP8266SendTime = 0;
        }
    }
    // 第三步，返回接受成功信息
    else if(DevParam.NetWorkFlow == 3)    {
        ESP8266_SendStr("{\"code\":1,\"msg\":\"getWifiInfo:success\",\"data\":{\"productId\":\"15083491\",\"mac\":\"%s\",\"deviceName\":\"%s\",\"operator\":\"xiaoyi\"}}", DevParam.macbuf, DevParam.macbuf);
        DevParam.NetWorkFlow = 4;
    }
	  #ifdef MQTT_SCode
    // 第四步，连接家庭无线
    else if(DevParam.NetWorkFlow == 4)    {
        ESP8266_STAInit();
        while(ESP8266_STAConnect((char *)Wssid, (char *)Wpassword));
        DevParam.NetWorkFlow = 5;
    }
		#endif
		
    // 第五步，配置MQTT
    else if(DevParam.NetWorkFlow == 5)    {
        /*********模拟MQTT指令**************/
        /********************************/
        #ifdef MQTT_SCode
        MQTT_ConnectData.keepAlive         = 120;         // 心跳包，单位秒
        strcpy( MQTT_ConnectData.clientID, MQTT_ClientID);// 客户端ID
        strcpy( MQTT_ConnectData.username, MQTT_Username);// 用户名
        strcpy( MQTT_ConnectData.password, MQTT_Password);// 密码
        
        /***连接服务器***/
        while(ESP8266_SendCMD((uint8_t *)"AT+CIPMUX=0\r\n",(uint8_t *)"OK",200));
        memset((void *)PublishData, 0, sizeof(PublishData));
        sprintf((char *)PublishData,"AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", MQTT_ServerIP, MQTT_ServerPort);
        while(ESP8266_SendCMD((uint8_t *)PublishData, (uint8_t *)"OK", 500)); 
        while(ESP8266_SendCMD((uint8_t *)"AT+CIPMODE=1\r\n",(uint8_t *)"OK",200));
        while(ESP8266_SendCMD((uint8_t *)"AT+CIPSEND\r\n",(uint8_t *)"OK",200));
    
        /***登录服务器***/
        while(MQTTSCode_Connect()); 
        
        /***订阅服务器对状态设置***/
        memset((void *)PublishData, 0, sizeof(PublishData));
        sprintf((char *)PublishData,"status/set/%s", (char *)DevParam.macbuf);
        while(MQTTSCode_Subscribe((char *)PublishData, 0, 12)); 
		
		/****创建设备信息，如果已经创建了也不影响***/
		memset((void *)PublishData, 0, sizeof(PublishData));
		len = sprintf((char *)PublishData,"{\"deviceNum\":\"%s\",\"categoryId\":%d,\"firmwareVersion\":%s,\"ownerId\":%s}",\
				(char *)DevParam.macbuf, MQTT_CATEGORY, MQTT_VERSION, MQTT_OWNER_ID);
		if(len > 0)
		{
			while(MQTTSCode_Publish("device_info", (char *)PublishData, len, 0, 1, 10));
			memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
		}
        #endif
        DevParam.NetWorkFlow = 10;
        /********************************/
    }
    // 第十步，发布消息
    else if(DevParam.NetWorkFlow == 10)    {
        /********************************/
        #ifdef MQTT_SCode
        if(DevParam.MQTTSendTime >= 2000)
        {
            // 定时发布设备状态
			memset((void *)PublishData, 0, sizeof(PublishData));
			len = sprintf((char *)PublishData,"{\"deviceNum\":\"%s\",\"isOnline\":1,\"airTemperature\":%d,\"remark\":{\"pm2_5\":%d,\"pm1_0\":%d,\"pm10\":%d}}",\
					(char *)DevParam.macbuf, DevParam.Temperature, DevParam.PM2_5, DevParam.PM1_0, DevParam.PM10);  
			if(len > 0)
			{
					while(MQTTSCode_Publish("status", (char *)PublishData, len, 0, 1, 10));
					memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
			}
			DevParam.MQTTSendTime = 0;
        }
        #endif
        /********************************/        
    }
}
char mystrstr(char *haystack, char *needle) {
    uint16_t i = 0,j = 0;
    for(i = 0;;i++)
    {
        if(haystack[i]==needle[0])
        {
            for(j = 0;;j++)
            {
                if(haystack[i+j]==needle[j])
                {
                    if(needle[j+1] == '\0')
                        return 1;
                }
                else  
                    break;
            }
        }
        if((haystack[i]=='\r') && (haystack[i+1]=='\n'))
        {
            return 0;
        }        
    }
}

cJSON *root = NULL;
char JsonString[500] = {0};
uint16_t i = 0,j = 0,slen = 0,Nport = 0;
void ESP8266_NetReceiveInfor(void) 
{
    if(UART2ReadFlag&0x8000)
    {
		if (strstr((char *)UART2ReadBuf, "ready"))
		{
			memset(UART2ReadBuf, 0x00, sizeof(UART2ReadBuf));
		}
		//判断当前网络状态
		if(strstr((char *)UART2ReadBuf, "WIFI GOT IP"))
		{
			ESP8266_NetStatus(WIFI_GOT_IP);
		}
		else if(strstr((char *)UART2ReadBuf, "WIFI DISCONNECT"))
		{
			ESP8266_NetStatus(WIFI_DISCONNECT);
		}
        // SoftAP配网解析
        if( (DevParam.NetWorkFlow==2) || (DevParam.NetWorkFlow==20)) 				{
            // {"port":60156,"ssid":"xxx","password":"xxxx"}
            if(UART2ReadBuf[0] == '{')            
			{
                memset(JsonString, 0, sizeof(JsonString));
                for(i = 0;;i++)
                {
                    JsonString[i] = UART2ReadBuf[i];
                    if(UART2ReadBuf[i] == '}')
                    {
                        // 解析数据
                        root = cJSON_Parse((char *)JsonString); 
                        if(root == NULL)
                        {
                            memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
                            UART3ReadFlag = 0;
                            return;
                        }
                        if(DevParam.NetWorkFlow==2)
                        {
							//////////////////////////////////////////
							/*判断是否需要进入AIRKISS配网阶段*/
							if (strstr((char *)UART2ReadBuf, "getMac:success"))
							{
								DevParam.NetWorkFlow = 1;
							}
							//////////////////////////////////////////
//                            Nport = cJSON_GetObjectItem(root, "port")->valuedouble;
//                            if(Nport==60156)
//                            {
//                                sprintf((char *)Wssid,    "%s",cJSON_GetObjectItem(root, "ssid")->valuestring);
//                                sprintf((char *)Wpassword,"%s",cJSON_GetObjectItem(root, "password")->valuestring);
//                                // 进入无线第三步
//                                DevParam.NetWorkFlow = 3;
//                            }
                        }
                        else if(DevParam.NetWorkFlow==20)
                        {
                            Nport = cJSON_GetObjectItem(root, "port")->valuedouble;
                            if(Nport==60156)
                            {
                                sprintf((char *)MQTT_ClientID,"%s",cJSON_GetObjectItem(root, "deviceId")->valuestring);
                                sprintf((char *)MQTT_Password,"%s",cJSON_GetObjectItem(root, "token")->valuestring);
                                //进入无线第三步
                                DevParam.NetWorkFlow = 21;
                            }
                        }                       
                        cJSON_Delete(root);                                          
                        break;
                    }
                }
            } 
        }
		//一键配网	
		else if (DevParam.NetWorkFlow == 212)  	{
			if(strstr((char *)UART2ReadBuf, "WIFI GOT IP"))            
			{
//						//配网成功，关闭一键配网
//						while(ESP8266_SendCMD((uint8_t *)"AT+CWSTOPSMART\r\n",(uint8_t *)"OK",200))
//						{
//							Delay_ms(10);
//						}
				DevParam.NetWorkFlow = 5;
			}
		}
        // 服务器解析
        if(DevParam.NetWorkFlow == 10)       {
            #ifdef MQTT_SCode            
            slen = UART2ReadFlag&(~(1 << 15));
            UART2ReadBuf[slen]   = 0x0D;
            UART2ReadBuf[slen+1] = 0x0A;
            /**********设置状态*********/
            memset((void *)PublishData, 0, sizeof(PublishData));
            sprintf((char *)PublishData,"status/set/%s", (char *)DevParam.macbuf);
            if(mystrstr((char *)UART2ReadBuf, (char *)PublishData)==1)   {
                memset(JsonString, 0, sizeof(JsonString));
                for(i = 0;;i++)
                {
                    if(UART2ReadBuf[i]=='{')
                    {
                        for(j = 0;;j++)
                        {
                            JsonString[j] = UART2ReadBuf[j+i];
                            if( (UART2ReadBuf[j+i]=='}') && (UART2ReadBuf[j+i+1]==0x0D) && (UART2ReadBuf[j+i+2]==0x0A))
                            {
                                // 解析数据
                                root = cJSON_Parse((char *)JsonString); 
                                if(root == NULL)
                                {
                                    memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
                                    UART3ReadFlag = 0;
                                    return;
                                }
                                
                                DevParam.ServerRelay= cJSON_GetObjectItem(root, "relayStatus")->valuedouble;
                                DevParam.ServerRed  = cJSON_GetObjectItem(root, "red")->valuedouble;
                                
                                cJSON_Delete(root);                       
                                memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
                                UART3ReadFlag = 0;                     
                                return;
                            }   
                        }
                    }
                } 
            }            
            #endif
        }
        memset((void *)UART2ReadBuf, 0, sizeof(UART2ReadBuf));
        UART2ReadFlag = 0;        
    }
}

